using System.Collections.Generic;
using System.Linq;
using UnityEngine;

/// <summary>
/// 遗物（先贤庇佑）的数据定义
/// </summary>
[System.Serializable]
public class RelicData
{
    [Header("基础信息")]
    public string id;       // 唯一ID 
    public string name;     // 显示名称
    [Header("描述")]
    [TextArea(3, 5)]
    public string description;
    // TODO: 可以添加稀有度、美术资源引用等
    // public Rarity rarity; 
    // public Sprite icon;
}

/// <summary>
/// 遗物配置 ScriptableObject
/// 职责：存储游戏中所有可用的遗物数据
/// </summary>
[CreateAssetMenu(fileName = "RelicConfig", menuName = "山中战纪/遗物配置")]
public class RelicConfig : ScriptableObject
{
    [Header("遗物数据库")]
    [SerializeField]
    private List<RelicData> allRelics = new List<RelicData>();
    // 缓存字典以便快速查找
    private Dictionary<string, RelicData> _relicDatabase;
    /// <summary>
    /// 初始化数据库（字典）
    /// </summary>
    private void OnEnable()
    {
        _relicDatabase = new Dictionary<string, RelicData>();
        foreach (var relic in allRelics)
        {
            if (!_relicDatabase.ContainsKey(relic.id))
            {
                _relicDatabase.Add(relic.id, relic);
            }
            else
            {
                Debug.LogWarning($"[RelicConfig] 遗物ID重复: {relic.id}");
            }
        }
    }
    /// <summary>
    /// 根据ID获取遗物数据
    /// </summary>
    public RelicData GetRelicData(string relicId)
    {
        if (_relicDatabase == null)
        {
            OnEnable(); // 确保字典已初始化
        }

        if (_relicDatabase.TryGetValue(relicId, out RelicData data))
        {
            return data;
        }
        
        Debug.LogError($"[RelicConfig] 未找到ID为 {relicId} 的遗物数据");
        return null;
    }

    /// <summary>
    /// 获取所有遗物的数据（用于商店、图鉴等）
    /// </summary>
    public List<RelicData> GetAllRelics()
    {
        return allRelics;
    }
}